/*
 * @Author: 杨上漂
 * @Date: 2024-10-13 10:27:42
 * @LastEditors: 杨上漂
 * @LastEditTime: 2024-10-15 15:21:52
 * @FilePath: \gwlc\assets\scripts\Arrow.ts
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
import { _decorator, BoxCollider2D, Collider, Collider2D, Component, Contact2DType, Director, director, ITriggerEvent, log, Node, v2, v3 } from "cc";

import { gameMgr } from '../mainScene/gameMgr';
import { lookAtTarget } from '../utils/utils';
import { Enemy } from '../mainScene/Enemy';
import { playerSkillTypeEnum } from '../Enum';
import { Player } from "../mainScene/Player";
import { PoolMgr } from "../PoolMgr";

const { ccclass, property } = _decorator;

@ccclass('Arrow')
export class Arrow extends Component {

    attackTargetPos=null;

    tempPos=v3();

    init(_attackTargetPos) {
     this.poolReset();
        this.attackTargetPos=_attackTargetPos;

        let collider=this.node.getComponent(BoxCollider2D);
        collider.on(Contact2DType.BEGIN_CONTACT, this.onTriggerEnterFuc, this);

        lookAtTarget(this.attackTargetPos,this.node,50);
        gameMgr.ins.targetLineMove(this.attackTargetPos,this.node,Player.ins.arrowSpeed)

    }
    poolReset() {//重新设置，针对预制体池
        this.attackTargetPos = null;
        this.tempPos = v3();
    }

    onTriggerEnterFuc(self: Collider, other: Collider) {
            if (other.node.getComponent(BoxCollider2D).tag==1) {
                director.once(Director.EVENT_AFTER_PHYSICS,()=>{
                    this.recycle();

                    other.node.getComponent(Enemy).hurt(playerSkillTypeEnum.normalArrow);
                },this)
            }
    }

    recycle(){
        let collider=this.node.getComponent(BoxCollider2D);
        collider.off(Contact2DType.BEGIN_CONTACT, this.onTriggerEnterFuc, this);
        PoolMgr.ins.poolPut(this.node.name,this.node);
    }
 
 
    
 
}


